﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;

namespace DEMI_webapp
{
	public partial class ProfilUzytkownika : System.Web.UI.Page
	{
		System.Configuration.ConnectionStringSettings conString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["conString"];
		SqlConnection con;
		int roznica = 0;
		int staraIlosc = 0;
		int aktualnaIlosc = -1;
		int aktualnaCena = -1;
		string lastValue = "";
		string lastCol = "";
		string col1 = "lightblue";
		string col2 = "lightgreen";

		protected void Page_Load (object sender, EventArgs e)
		{
			con = new SqlConnection(conString.ConnectionString);
			con.Open();
		}

		protected void GridView1_RowUpdated (object sender, GridViewUpdatedEventArgs e)
		{
			staraIlosc = Convert.ToInt32(e.OldValues[9]);
			int nowaIlosc = Convert.ToInt32(e.NewValues[0]);
			int idZamowioneKapelusze = Convert.ToInt32(e.OldValues[1]);
			int idZamowienia = Convert.ToInt32(e.OldValues[0]);
			int idKapelusza = Convert.ToInt32(e.OldValues[7]);

			if (isUpdatePossible(idKapelusza, nowaIlosc))
				Update(idZamowioneKapelusze, idKapelusza, nowaIlosc, idZamowienia);
		}

		private bool isUpdatePossible (int idKapelusza, int nowaIlosc)
		{
			SqlCommand check = new SqlCommand("SELECT ilosc, cena FROM Kapelusze WHERE id=@ID", con);
			SqlParameter paramID = new SqlParameter("@ID", SqlDbType.Int);
			paramID.Value = idKapelusza;
			check.Parameters.Add(paramID);
			SqlDataReader reader = check.ExecuteReader();

			if (reader.Read())
			{
				try
				{
					aktualnaIlosc = Convert.ToInt32(reader["ilosc"]);
					aktualnaCena = Convert.ToInt32(reader["cena"]);
				}
				catch (Exception e) { }
				reader.Close();
			}

			roznica = staraIlosc - nowaIlosc;

			if ((roznica < 0 && roznica * -1 < aktualnaIlosc) || (roznica >= 0))
				return true;

			return false;
		}

		private void Update (int idZamowioneKapelusze, int idKapelusza, int nowaIlosc, int idZamowienia)
		{
			SqlCommand updateZamowioneKapelusze = new SqlCommand("UPDATE Zamowione_kapelusze SET ilosc_kapeluszy = @ILOSC WHERE id = @ID", con);
			SqlParameter paramID = new SqlParameter("@ID", SqlDbType.Int);
			paramID.Value = idZamowioneKapelusze;
			SqlParameter paramILOSC = new SqlParameter("@ILOSC", SqlDbType.Int);
			paramILOSC.Value = nowaIlosc;
			updateZamowioneKapelusze.Parameters.Add(paramID);
			updateZamowioneKapelusze.Parameters.Add(paramILOSC);
			updateZamowioneKapelusze.ExecuteNonQuery();

			SqlCommand updateKapelusze = new SqlCommand("UPDATE Kapelusze SET ilosc = @ILOSC WHERE id = @ID", con);
			paramID = new SqlParameter("@ID", SqlDbType.Int);
			paramID.Value = idKapelusza;
			paramILOSC = new SqlParameter("@ILOSC", SqlDbType.Int);
			paramILOSC.Value = aktualnaIlosc + roznica;
			updateKapelusze.Parameters.Add(paramID);
			updateKapelusze.Parameters.Add(paramILOSC);
			updateKapelusze.ExecuteNonQuery();

			int cenaZamowienia = -1;
			SqlCommand selectCenaZamowienia = new SqlCommand("SELECT cena FROM Zamowienia WHERE id = @ID", con);
			paramID = new SqlParameter("@ID", SqlDbType.Int);
			paramID.Value = idZamowienia;
			selectCenaZamowienia.Parameters.Add(paramID);
			SqlDataReader reader = selectCenaZamowienia.ExecuteReader();
			if (reader.Read())
			{
				try
				{
					cenaZamowienia = Convert.ToInt32(reader["cena"]);
				}
				catch (Exception e) { }
			}
			reader.Close();

			SqlCommand updateZamowienia = new SqlCommand("UPDATE Zamowienia SET cena = @CENA WHERE id = @ID", con);
			paramID = new SqlParameter("@ID", SqlDbType.Int);
			paramID.Value = idZamowienia;
			SqlParameter paramCENA = new SqlParameter("@CENA", SqlDbType.Int);
			paramCENA.Value = cenaZamowienia - aktualnaCena * roznica;
			updateZamowienia.Parameters.Add(paramID);
			updateZamowienia.Parameters.Add(paramCENA);
			updateZamowienia.ExecuteNonQuery();

			GridView1.DataBind();
		}

		protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)
		{
			string newValue = "";

			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				string value = e.Row.Cells[2].Text;
				if (value.Contains(","))
					e.Row.Cells[2].Text = value.Split(',')[0] + "<br />" + value.Split(',')[1];

				newValue = e.Row.Cells[0].Text;

				if (lastValue == "")
				{
					lastValue = e.Row.Cells[0].Text;
					lastCol = col1;
				}
				else if (lastValue != newValue)
				{
					lastCol = lastCol == col1 ? col2 : col1;
				}
				e.Row.BackColor = Color.FromName(lastCol);
				e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'");
				e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='" + lastCol + "'");

				lastValue = newValue;
			}
		}

		protected void GridView1_RowEditing (object sender, GridViewEditEventArgs e)
		{
			GridView g = (GridView)sender;
			int idZamowienia = Convert.ToInt32(g.Rows[e.NewEditIndex].Cells[0].Text);

			SqlCommand selectEdycja = new SqlCommand("SELECT DATEDIFF(day, (SELECT data_zlozenia FROM Zamowienia WHERE id = @ID), GETDATE()) AS diff FROM Zamowienia WHERE id = @ID and data_zrealizowania IS NULL", con);
			SqlParameter paramID = new SqlParameter("@ID", SqlDbType.Int);
			paramID.Value = idZamowienia;
			selectEdycja.Parameters.Add(paramID);
			SqlDataReader reader = selectEdycja.ExecuteReader();
			int diff = -1;
			if (reader.Read())
			{
				diff = Convert.ToInt32(reader["diff"]);
				if (diff <= 1) return;
			}
			reader.Close();

			string msg = "Nie ma możliwości edycji zamówienia. Minęło już " + diff + " dni od złożenia zamówienia";
			if (diff == -1)
				msg = "Zamówienie zostało już zrealizowane, nie ma możliwości zmiany zamówienia!";

			Label lbl = new Label();
			lbl.Text = "<script type=\"text/javascript\">alert('" + msg + "');</script>";
			Page.Controls[0].Controls.AddAt(1, lbl);
			e.Cancel = true;
			return;
		}
	}
}